package com.jianyun.jyzs.utils;

import android.annotation.SuppressLint;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

@SuppressLint({ "DefaultLocale", "SimpleDateFormat" })
public class DateTime {

	public static String MD(long startTime) {
		Calendar st = Calendar.getInstance();
		st.setTimeInMillis(startTime);
		return (st.get(Calendar.MONTH)+1)+"月"+st.get(Calendar.DAY_OF_MONTH)+"日 ";
	}
	public static String YMD(long startTime){
		Calendar st = Calendar.getInstance();
		st.setTimeInMillis(startTime);
		return st.get(Calendar.YEAR)+"年"+(st.get(Calendar.MONTH)+1)+"月"+st.get(Calendar.DAY_OF_MONTH)+"日 ";
	}

	public static String yyyy_MM_dd() {
		return new SimpleDateFormat("yyyy-MM-dd", Locale.CHINA).format(new Date());
	}
	public static String msecToTime(int paramInt){
		paramInt /= 1000;
		int i = paramInt / 60;
		int j = 0;
		if (i >= 60){
			j = i / 60;
			i %= 60;
		}
		int k = paramInt % 60;
		return String.format("%02d:%02d", new Object[] { Integer.valueOf(i), Integer.valueOf(k) });
	}
	private static final long ONE_MINUTE = 60;
	private static final long ONE_HOUR = 3600;
	private static final long ONE_DAY = 86400;
	private static final long ONE_MONTH = 2592000;
	private static final long ONE_YEAR = 31104000;

	public static String getMMDD(Date date){
		int year = date.getYear();
		int month = date.getMonth()+1;
		int day = date.getDay();
		String md = year+"年"+month+"月"+day+"号";
		return md;
	}


	public static String getFriendlytime(Date d){
		long delta = (new Date().getTime()-d.getTime())/1000;
		if(delta<=0)return d.toLocaleString();
		if(delta/(60*60*24*365) > 0) return delta/(60*60*24*365) +"年前";
		if(delta/(60*60*24*30) > 0) return delta/(60*60*24*30) +"个月前";
		if(delta/(60*60*24*7) > 0)return delta/(60*60*24*7) +"周前";
		if(delta/(60*60*24) > 0) return delta/(60*60*24) +"天前";
		if(delta/(60*60) > 0)return delta/(60*60) +"小时前";
		if(delta/(60) > 0)return delta/(60) +"分钟前";
		return "刚刚";
	}

	/*
	 * 根据传入的时间,计算是今天,昨天,星期还是月还是年
	 */
	@SuppressLint("SimpleDateFormat")
	public static String dateToString(long startTime) {
		long currentTimeMillis = System.currentTimeMillis();
		try {
			if(startTime <= 0){
				return "";
			}
			String hs = new SimpleDateFormat("HH:mm").format(new Date(startTime));
			long nd = 1000 * 24 * 60 * 60;// 一天的毫秒数

			long diff;
			diff = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(yyyy_MM_dd()+" 23:59:59").getTime() - startTime;
			long day = diff / nd;// 计算差多少天
			if(day == 0){
				return hs;
			}else if(day == 1){
				return "昨天 "+hs;
			}else{
				long dayCount = dateDiff(startTime, currentTimeMillis,"d");
				final Calendar c = Calendar.getInstance();
				c.setTimeInMillis(currentTimeMillis);
				int week = c.get(Calendar.DAY_OF_WEEK);
				Calendar st = Calendar.getInstance();
				st.setTimeInMillis(startTime);
				if(week == 4 && dayCount <= 3){
					return "星期一 "+hs;
				}else if(week == 5 && dayCount <= 4){
					return "星期"+getWeek(st.get(Calendar.DAY_OF_WEEK))+" "+hs;
				}else if(week == 6 && dayCount <= 5){
					return "星期"+getWeek(st.get(Calendar.DAY_OF_WEEK))+" "+hs;
				}else if(week == 7 && dayCount <= 6){
					return "星期"+getWeek(st.get(Calendar.DAY_OF_WEEK))+" "+hs;
				}else if(week == 1 && dayCount < 7){
					return "星期"+getWeek(st.get(Calendar.DAY_OF_WEEK))+" "+hs;
				}else if(c.get(Calendar.YEAR) == st.get(Calendar.YEAR)){
					return (st.get(Calendar.MONTH)+1)+"月"+st.get(Calendar.DAY_OF_MONTH)+"日 "+hs;
				}else{
					return st.get(Calendar.YEAR)+"年"+(st.get(Calendar.MONTH)+1)+"月"+st.get(Calendar.DAY_OF_MONTH)+"日 "+hs;
				}
			}
		} catch (Exception e) {
		}
		return "";
	}

	@SuppressLint("SimpleDateFormat")
	public static Long dateDiff(String startTime, String endTime, String str) {
		if(str == null || str.trim().equals("")){
			return 0L;
		}
		// 按照传入的格式生成一个simpledateformate对象
		long nd = 1000 * 24 * 60 * 60;// 一天的毫秒数
		long nh = 1000 * 60 * 60;// 一小时的毫秒数
		long nm = 1000 * 60;// 一分钟的毫秒数
		long ns = 1000;// 一秒钟的毫秒数
		long diff = 0;
		long day = 0;
		long hour = 0;
		long min = 0;
		long sec = 0;
		// 获得两个时间的毫秒时间差异
		try {
			diff = new SimpleDateFormat("yyyy-MM-dd").parse(endTime).getTime() - new SimpleDateFormat("yyyy-MM-dd").parse(startTime).getTime();
		} catch (Exception e) {
			return 0L;
		}
		day = diff / nd;// 计算差多少天
		hour = diff % nd / nh + day * 24;// 计算差多少小时
		min = diff % nd % nh / nm + day * 24 * 60;// 计算差多少分钟
		sec = diff % nd % nh % nm / ns;// 计算差多少秒

		if (str.equalsIgnoreCase("d")) {
			return day;
		} else if (str.equalsIgnoreCase("h")) {
			return hour;
		} else if (str.equalsIgnoreCase("m")) {
			return min;
		} else{
			return sec;
		}
	}

	public static Long dateDiff(long startTime, long endTime, String str) {
		if(str == null || str.trim().equals("")){
			return 0L;
		}
		// 按照传入的格式生成一个simpledateformate对象
		long nd = 1000 * 24 * 60 * 60;// 一天的毫秒数
		long nh = 1000 * 60 * 60;// 一小时的毫秒数
		long nm = 1000 * 60;// 一分钟的毫秒数
		long ns = 1000;// 一秒钟的毫秒数
		long diff;
		long day = 0;
		long hour = 0;
		long min = 0;
		long sec = 0;
		// 获得两个时间的毫秒时间差异
		diff = endTime - startTime;
		day = diff / nd;// 计算差多少天
		hour = diff % nd / nh + day * 24;// 计算差多少小时
		min = diff % nd % nh / nm + day * 24 * 60;// 计算差多少分钟
		sec = diff % nd % nh % nm / ns;// 计算差多少秒

		if (str.equalsIgnoreCase("d")) {
			return day;
		} else if (str.equalsIgnoreCase("h")) {
			return hour;
		} else if (str.equalsIgnoreCase("m")) {
			return min;
		} else{
			return sec;
		}
	}

	public static String getWeek(int wk) {
		if (wk == 1) {
			return "日";
		} else if (wk == 2) {
			return "一";
		} else if (wk == 3) {
			return "二";
		} else if (wk == 4) {
			return "三";
		} else if (wk == 5) {
			return "四";
		} else if (wk == 6) {
			return "五";
		} else if (wk == 7) {
			return "六";
		}
		return "";
	}

	public static String yyyyMMdd() {
		return new SimpleDateFormat("yyyyMMdd", Locale.CHINA).format(DateTime.getClock());
	}
	public static Long getClock() {
		return System.currentTimeMillis() ;
	}

	public static Long getmillionTime(String date){
		long millionSeconds = 0;
		try {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			 millionSeconds = sdf.parse(date).getTime();//毫秒
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return millionSeconds;
		//return new SimpleDateFormat("yyyyMMddhhmmss").parse(date).getTime();
	}

	public static String getStringDate(Long l){
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		String strdate = format.format(new Date(l));
		return strdate;
	}

	@SuppressLint("SimpleDateFormat")
	public static String specialDateToString(long startTime) {
		try {
			if(startTime <= 0){
				return "";
			}
			long nd = 1000 * 24 * 60 * 60;// 一天的毫秒数
			long diff;
			diff = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(yyyy_MM_dd()+" 23:59:59").getTime() - startTime;
			long day = diff / nd;// 计算差多少天
			if(day == 0){
				return "今天";
			}else if(day == 1){
				return "昨天";
			}else{
				Calendar st = Calendar.getInstance();
				st.setTimeInMillis(startTime);
				return st.get(Calendar.DAY_OF_MONTH)+","+(st.get(Calendar.MONTH)+1);
			}
		} catch (Exception e) {
		}
		return "";
	}


	public static String getYYMMDD(Date date) {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		String strdate = format.format(date);
		return strdate;
	}
}
